package com.aistar.service.impl;

import com.aistar.mapper.MemberMapper;
<<<<<<< HEAD
import com.aistar.mapper.jdbc.MemberMapperImpl;
import com.aistar.pojo.Member;
import com.aistar.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * dependency injection [DI]
 * service 依赖于 mapper
 *
 * mapper 对象 如何注入到 service中？
 *
 * （1）构造器注入
 * （2）setter 注入
 */
@Service //<bean id="memberServiceImpl" class="com.aistar.service.impl.MemberServiceImpl">
public class MemberServiceImpl implements MemberService {


    @Autowired
    private  MemberMapper mapper ;   // <bean id="memberMapperImpl" class="..MemberMapperImpl">

    public MemberServiceImpl(){}
    public MemberServiceImpl(MemberMapper mapper) {
        this.mapper = mapper;
    }


    public void setMapper(MemberMapper mapper) {
        this.mapper = mapper;
        System.out.println("mapper = " + mapper);
=======
import com.aistar.pojo.Member;
import com.aistar.pojo.MemberExample;
import com.aistar.service.MemberService;
import com.aistar.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public boolean save(Member member) {// member 只有手机号
        //手机号码 已经 注册过
        if( this.getByTelno(member.getMemberTelno())!=null){
            return false;
        }else{ //没有注册过
            return this.memberMapper.insert(member) >0;
        }

>>>>>>> 79297cf758ff388ed3ea7d02640b718856076aad

    }

    @Override
<<<<<<< HEAD
    public Member getById(int id) {
        System.out.println("MemberServiceImpl getById");
        return  mapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean save(Member member) {
        System.out.println("MemberServiceImpl save");
        int rows = mapper.save(member);
        return rows>0;
    }

    @Override
    public boolean update(Member member) {
        System.out.println("MemberServiceImpl update");
        int rows = mapper.update(member);
        return rows>0;
    }

    @Override
    public boolean delete(int id) {
        System.out.println("MemberServiceImpl delete");
        int rows = mapper.delete(id);
        return rows>0;
    }

    @Override
    public boolean getByOthers(String memberName, String memberPwd) {
        System.out.println("MemberServiceImpl getByOthers");
        return mapper.selectByOthers(memberName,memberPwd)>0;
    }


    @Override
    public void updateScore(Member m1, Member m2) {
        m1.setScore(m1.getScore()-10);
        m2.setScore(m2.getScore()+20);
        mapper.updateScore(m1);
        //int a = 10/0;
        mapper.updateScore(m2);

        System.out.println("member1 ="+ m1.getScore());
        System.out.println("member2 =" + m2.getScore());
=======
    public boolean update(Member member) {
        return this.memberMapper.updateByPrimaryKey(member) >0;
    }

    @Override
    public Member getById(int memberId) {

        return    memberMapper.selectByPrimaryKey(memberId);
    }

    @Override
    public JsonResult getByTelno(Long memberTelno) {

        Member member = null;

        MemberExample example = new MemberExample();
        MemberExample.Criteria criteria = example.createCriteria();
        criteria.andMemberTelnoEqualTo(memberTelno);
        criteria.andStatusEqualTo(0);
        List<Member> memberList = memberMapper.selectByExample(example);//select * from member where telno = ? and status =0

        if(memberList.size()!=0) {
            member = memberList.get(0);
            return  new JsonResult(0,"查询到该用户,手机号已使用",member);
        }else{
            return  new JsonResult(1,"没有查询到该手机用户",member);
        }



    }

    @Override
    public Member login(String username, String password) {
        Member member = null;

        MemberExample example = new MemberExample();
        MemberExample.Criteria criteria = example.createCriteria();
        criteria.andMemberNameEqualTo(username);
        criteria.andPasswordEqualTo(password);
        criteria.andStatusEqualTo(0);
        List<Member> memberList = memberMapper.selectByExample(example);          //select * from member where username = ? and password = ? and status =0

        if(memberList.size()!=0)
            member = memberList.get(0);
        return member;
    }

    @Override
    public Member loginByTelno(Long memberTelno, String password) {
        Member member = null;

        MemberExample example = new MemberExample();
        MemberExample.Criteria criteria = example.createCriteria();
        criteria.andMemberTelnoEqualTo(memberTelno);
        criteria.andPasswordEqualTo(password);
        criteria.andStatusEqualTo(0);
        List<Member> memberList = memberMapper.selectByExample(example);          //select * from member where username = ? and password = ? and status =0

        if(memberList.size()!=0)
            member = memberList.get(0);
        return member;
>>>>>>> 79297cf758ff388ed3ea7d02640b718856076aad

    }
}
